<template>
  <a-drawer v-model:open="open" :mask-closable="false" class="custom-class" root-class-name="root-class-name"
    width="70%" :title="props.type == 'add' ? '新增食物' : '编辑食物'" placement="right" @close="closebtn">
    <a-form ref="formRef" :model="formState.info" :rules="rules" layout="vertical">
      <a-form-item ref="icon" label="封面" name="icon">
        <uploadImg v-model:value="formState.info.cover" />
      </a-form-item>
      <a-form-item ref="name" label="名称" name="name">
        <a-input v-model:value="formState.info.name" />
      </a-form-item>
      <a-form-item ref="categoryIds" label="分类" name="categoryIds">
        <a-cascader mode="multiple" v-model:value="formState.info.categoryIds" style="width: 100%"
          max-tag-count="responsive" :options="optinoslist" placeholder="请选择分类"></a-cascader>
      </a-form-item>
      <a-form-item ref="name" label="描述" name="name">
        <a-textarea v-model:value="formState.info.description" placeholder="请填写描述" :auto-size="{ minRows: 3 }" />
      </a-form-item>

      <!-- <a-form-item ref="name" label="制作过程" name="pairings">
      </a-form-item> -->
      <recipeList v-model:value="formState.info.pairings" :type="props.type" />



      <a-form-item ref="instructionContent" label="制作说明" name="instructionContent">
        <editorView v-model:value="formState.info.instructionContent" />
      </a-form-item>
    </a-form>
    <template #footer>
      <div style="display: flex; justify-content: flex-end">
        <a-button style="margin-right: 8px" @click="closebtn">取消</a-button>
        <a-button type="primary" :loading="loading" @click="infobtn">确定</a-button>
      </div>
    </template>
  </a-drawer>
</template>

<script setup lang="ts">
import { computed, reactive, ref, watch, inject } from "vue";

import { message } from "ant-design-vue";

import * as api from "#/api/core/repice";
import uploadImg from '#/components/uploadImg/index.vue';
import editorView from '#/components/editorView.vue';

import recipeList from './recipeList.vue'





const props = defineProps({
  value: {
    type: Boolean,
    default: false,
  },
  type: {
    type: String,
    default: "add",
  },
  editdata: {
    type: Object,
    default: () => { },
  },
  catelist: {
    type: Array,
    default: () => [],

  }
});

const emits = defineEmits(["update:value", "submit", "close"]);


const optinoslist = computed({
  get() {
    return props.catelist
  },
  set() {

  }
})



const formRef = ref();
const rules = {
  name: [
    {
      required: true,
      message: "请输入名称",
      trigger: "change",
    },
  ],
  categoryIds: [
    {
      required: true,
      message: "请选择分类",
      trigger: "change",
    },
  ],

};

const formState = reactive({
  info: {
    categoryIds: null,
    name: "",
    cover: '',
    description: "",
    instructionContent: '',
    pairings: []
  },
});

const loading = ref(false);

watch(
  () => props.value,
  async (newV) => {
    console.log("props", props.editdata);

    if (newV && props.type == "update") {


      let temp = await api.$getdesc(props.editdata.id, {})
      console.log('获取食谱详情', temp);
      formState.info.name = temp?.name
      formState.info.cover = temp?.cover
      formState.info.description = temp?.description
      formState.info.categoryIds = temp?.categories.map((e: any) => {
        return e.categoryId
      })
      formState.info.instructionContent = temp?.instruction?.content
      formState.info.pairings = temp?.pairings
      // formState.info.components = temp?.components



    } else {
      formState.info.name = ''
      formState.info.cover = ''
      formState.info.description = ''
      formState.info.categoryIds = null
      formState.info.instructionContent = ''
      formState.info.pairings = []

    }
  },
  {
    immediate: true,
  }
);

const open = computed({
  get() {
    return props.value;
  },
  set(val) {
    emits("update:value", val);
  },
});



const infobtn = () => {







  console.log('提交', formState.info);


  formRef.value
    .validate()
    .then(async () => {
      loading.value = true;
      try {
        if (props.type == "add") {
          await api.$createrecipe({
            ...formState.info,
          });
          message.success("添加成功");

          open.value = false;
          loading.value = false;
          emits("submit");
        } else {
          await api.$updaterecipe(
            {
              ...formState.info,
            },
            props.editdata?.id
          );
          message.success("编辑成功");
          loading.value = false;

          open.value = false;
          emits("submit");
        }
        // debugger
      } catch (error) {
        console.log("error", error);

        loading.value = false;

      }
    })
    .catch((error: any) => {
      console.log("error", error);
    });
};

const closebtn = () => {
  open.value = false;
  // formState.info.name = "";
  // formState.info.description = "";
  // formState.info.icon = "";
  emits("close");
};


// 数据
const typelist = [
  {
    label: '打卡',
    value: 1
  },
  {
    label: '吃早饭',
    value: 2
  },
  {
    label: '锻炼',
    value: 3
  },
  {
    label: '多喝水',
    value: 4
  },
  {
    label: '早睡',
    value: 5
  },
  {
    label: '便便',
    value: 6
  },
  {
    label: '其他',
    value: 99
  },
]

const statuslist = [
  {
    label: '正常',
    value: 1
  },

  {
    label: '禁用',
    value: 2
  }
]
</script>
